package com.cpi.cornpp.grinddistribution.converter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.grinddistribution.dto.GrindDistributionDTO;
import com.cpi.cornpp.grinddistribution.exception.GrindDistributionException;
import com.cpi.cornpp.grinddistribution.view.GrindDistributionView;
import com.cs.common.utils.NumberUtil;

/**
 * Grind Distribution Converter
 * @author Edgar Joao
 *
 */
public class GrindDistributionConverter {	
    /**
     * Convert DTOs to Views
     * @param dto
     * @return
     * @throws GrindDistributionException
     */
	public GrindDistributionView convertDTOsToViews(GrindDistributionDTO dto) throws GrindDistributionException{
		GrindDistributionView view = null;
		try{
			if(dto != null){
				view = new GrindDistributionView();
				view.setCornTypeId(NumberUtil.convertInteger(dto.getCornTypeId()));
				view.setCode(dto.getCode());
				view.setDescription(dto.getDescription());
				view.setCornOrigin(dto.getCornOrigin());
				view.setGrindDistributionSettingVersionsId(NumberUtil.convertInteger(dto.getGrindDistributionSettingVersionsId()));
				view.setInventory(dto.getInventory());
				view.setDelivered(dto.getDelivered());
				view.setVersionId(dto.getVersionId());
			}
		}catch( Exception exception ){
			GrindDistributionException grindDistributionException=null;
			exception.printStackTrace(System.out);
			grindDistributionException= new GrindDistributionException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw grindDistributionException;
		}	
		return view;
	}
	/**
	 * Convert Views to DTOs
	 * @param view
	 * @return
	 * @throws GrindDistributionException
	 */
	public GrindDistributionDTO convertViewsToDTOs(GrindDistributionView view) throws GrindDistributionException{
		GrindDistributionDTO dto = null;
		try{
			if(view != null){
				dto = new GrindDistributionDTO();
				dto.setCornTypeId(new Integer(view.getCornTypeId()));
				dto.setCode(view.getCode());
				dto.setDescription(view.getDescription());
				dto.setCornOrigin(view.getCornOrigin());
				dto.setGrindDistributionSettingVersionsId(new Integer(view.getGrindDistributionSettingVersionsId()));
				dto.setInventory(view.getNewInventory());
				dto.setDelivered(view.getNewDelivered());
				dto.setVersionId(view.getVersionId());
			}			
		}catch( Exception exception ){
			GrindDistributionException grindDistributionException=null;
			exception.printStackTrace(System.out);
			grindDistributionException= new GrindDistributionException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw grindDistributionException;
		}	
		return dto;
	}
	/**
	 * Convert DTOs to Views List
	 * @param grindDistributionDTOsList
	 * @return
	 */
	public List convertListDTOsToViews(List grindDistributionDTOsList) throws GrindDistributionException{
		Iterator iterator = null;
		GrindDistributionDTO dto = null;
		GrindDistributionView view = null;
		List grindDistributionList = null;
		try{
			grindDistributionList = new ArrayList(0);
			iterator = grindDistributionDTOsList.iterator();
			while (iterator.hasNext()) {
				dto = (GrindDistributionDTO) iterator.next();
				view = this.convertDTOsToViews(dto);
				grindDistributionList.add(view);
			}			
		}catch( Exception exception ){
			GrindDistributionException grindDistributionException=null;
			exception.printStackTrace(System.out);
			grindDistributionException= new GrindDistributionException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw grindDistributionException;
		}	
		return grindDistributionList;
	}
	/**
	 * Convert Grind Distribution Views List
	 * @param grindDistributionViewsList
	 * @return
	 * @throws GrindDistributionException
	 */
	public List convertListViewsToDTOs(List grindDistributionViewsList) throws GrindDistributionException{
		Iterator iterator = null;
		GrindDistributionDTO dto = null;
		GrindDistributionView view = null;
		List grindDistributionList = null;
		try{
			grindDistributionList = new ArrayList(0);
			iterator = grindDistributionViewsList.iterator();
			while (iterator.hasNext()) {
				view = (GrindDistributionView) iterator.next();
				dto = this.convertViewsToDTOs(view);
				grindDistributionList.add(dto);
			}
		}catch( Exception exception ){
			GrindDistributionException grindDistributionException=null;
			exception.printStackTrace(System.out);
			grindDistributionException= new GrindDistributionException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw grindDistributionException;
		}	
		return grindDistributionList;
	}
	
}
